#include "mainwindow.h"
#include <QApplication>
#include <QFontDatabase>
#include <QIODevice>
#include <QFile>
#include <QDebug>
quint32 program_version = 0x01000101; //1.1.1

QString loadFontFamilyFromFiles(const QString &fontFileName)
{
    QString font = "";

    QFile fontFile(fontFileName);
    if(!fontFile.open(QIODevice::ReadOnly))
    {
        qDebug()<<"Open font file error";
        return font;
    }

    int loadedFontID = QFontDatabase::addApplicationFontFromData(fontFile.readAll());
    QStringList loadedFontFamilies = QFontDatabase::applicationFontFamilies(loadedFontID);
    if(!loadedFontFamilies.empty())
    {
        font = loadedFontFamilies.at(0);
    }
    fontFile.close();
    return font;
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    // Fixed font loads failed. (1/8 is a good day:D)
    // When you compile this project, you can see this file will be appended to resources.
    // Also you should do more steps when error above reproducing:
    // 1.Download the *.ttf/*.otf file
    // 2.Add qrc prefix(file: qrc.qrc).
    //<RCC>
    //  <qresource prefix="/img">
    //    <file>favicon.ico</file>
    // </qresource>
    // <qresource prefix="/fonts">
    //      <file>your-font-file.ttf</file>
    //  </qresource>
    // </RCC>
    // 3.Add your file downloaded.
    // 4.Good LUCK :D
    QString strFont0 =  loadFontFamilyFromFiles(":/fonts/fonts/NotoSansSC-Regular.otf");
    qDebug() << "strFont0 is: " << strFont0;
    if(strFont0 != "" && !strFont0.isNull() && !strFont0.isEmpty()){
        a.setFont(strFont0);
        QFont font;
        font.setFamily(strFont0);
        a.setFont(font);
        qApp->setFont(font);
    }
    w.show();
    return a.exec();
}
